<?php
/**
 * Action Controller
 * @author Tomas Srna
 * @package bakalarka
 */

class ActionController extends Controller
{
	protected $resource = 'action';
	/**
	 * Nastavenie jazyka
	 */
	function setLanguage()
	{
		if(array_key_exists('lang', $_GET))
			setcookie('lang', $_GET['lang'], time()+14*86400, APPBASE);
		else
			setcookie('lang', "", time()-86400, APPBASE);
		$redir = (array_key_exists('redir', $_GET) ?
					$_GET['redir'] : APPBASE);
		header("Location: $redir");
		exit();
	}
	
	function login()
	{
		$t = Template::instance();
		$t->auth = Auth::instance();
		
		if(getenv('REQUEST_METHOD') == 'POST')
		{
			if (!array_key_exists('username', $_POST) ||
				!array_key_exists('password', $_POST) || 
				$_POST['username'] == '' || $_POST['password'] == '')
			{
				$t->error = L::selected()->emptyRequiredField;
			}
			else
			{
				$a = Auth::instance();
				if(!$a->login($_POST['username'], $_POST['password']))
				{
					$t->error = L::selected()->loginIncorrect;
				}
				else
				{
					$redir = (array_key_exists('redir', $_POST) ? 
						$_POST['redir'] : APPBASE);
					header('Location: '.$redir);
					exit();
				}
			}
		}
		
		$t->title = L::selected()->login;
		$t->render('login');
	}
	
	function logout()
	{
		$a = Auth::instance();
		$a->logout();
		$redir = (array_key_exists('redir', $_GET) ? $_GET['redir'] : APPBASE);
		header('Location: '.$redir);
		exit();
	}
}
